package com.emerquim.persistence.dao;

// default package

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * PEntidad entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see .PEntidad
 * @author MyEclipse Persistence Tools
 */

public class PEntidadDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(PEntidadDAO.class);
	// property constants
	public static final String ENT_NOM = "entNom";
	public static final String ENT_DESC = "entDesc";
	public static final String ENT_DIR = "entDir";
	public static final String ENT_TELS = "entTels";
	public static final String ENT_FAX = "entFax";
	public static final String ENT_NIVEL = "entNivel";

	public void save(PEntidad transientInstance) {
		log.debug("saving PEntidad instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(PEntidad persistentInstance) {
		log.debug("deleting PEntidad instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public PEntidad findById(long id) {
		log.debug("getting PEntidad instance with id: " + id);
		try {
			PEntidad instance = (PEntidad) getSession().get("com.emerquim.persistence.dao.PEntidad", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(PEntidad instance) {
		log.debug("finding PEntidad instance by example");
		try {
			List results = getSession().createCriteria("PEntidad").add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding PEntidad instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from PEntidad as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByPropertyLike(String propertyName, Object value){
		log.debug("finding PEntidad instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from PEntidad as model where model." + propertyName + " like:value";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.setParameter("value", "%" + value + "%").list();
		} catch (RuntimeException re) {
			log.error("find by property like name failed", re);
			throw re;
		}
	}
	
	public List findByEntNom(Object entNom) {
		return findByProperty(ENT_NOM, entNom);
	}

	public List findByEntDesc(Object entDesc) {
		return findByProperty(ENT_DESC, entDesc);
	}

	public List findByEntDir(Object entDir) {
		return findByProperty(ENT_DIR, entDir);
	}

	public List findByEntTels(Object entTels) {
		return findByProperty(ENT_TELS, entTels);
	}

	public List findByEntFax(Object entFax) {
		return findByProperty(ENT_FAX, entFax);
	}

	public List findByEntNivel(Object entNivel) {
		return findByProperty(ENT_NIVEL, entNivel);
	}

	public List findAll() {
		log.debug("finding all PEntidad instances");
		try {
			String queryString = "from PEntidad";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public PEntidad merge(PEntidad detachedInstance) {
		log.debug("merging PEntidad instance");
		try {
			PEntidad result = (PEntidad) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(PEntidad instance) {
		log.debug("attaching dirty PEntidad instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(PEntidad instance) {
		log.debug("attaching clean PEntidad instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}